package com.banzhi.shop.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.banzhi.shop.R;

import static com.banzhi.shop.MyApp.context;

/**
 * Created by LeiHe on 2017/4/19.
 */

public class TitleBar extends LinearLayout {


    private TextView tv_title;
    private RadioButton btn_left;
    private Button btn_right;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取手机状态栏高度
        int statusBarHeight = 69;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        View view = LayoutInflater.from(context).inflate(R.layout.title_bar, this, true);
        view.setPadding(0, statusBarHeight, 10, 10);
        view.setBackgroundColor(getResources().getColor(R.color.themeColor));
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        btn_left = (RadioButton) view.findViewById(R.id.btn_left);
        btn_right = (Button) view.findViewById(R.id.btn_right);

        tv_title.setVisibility(INVISIBLE);
        btn_left.setVisibility(INVISIBLE);
        btn_right.setVisibility(INVISIBLE);

    }

    public void setTitle(String title) {
        tv_title.setVisibility(VISIBLE);
        tv_title.setText(title);
    }


    public void setBack(int imgId, OnClickListener listener) {
        btn_left.setVisibility(VISIBLE);
        Drawable imgDrawable = ContextCompat.getDrawable(context, imgId);
        imgDrawable.setBounds(0, 0, imgDrawable.getMinimumWidth(), imgDrawable.getMinimumHeight());
        btn_left.setCompoundDrawables(imgDrawable, null, null, null);
        btn_left.setOnClickListener(listener);
    }

    public void setRightSkip(int imgId, String content, OnClickListener listener) {
        btn_right.setVisibility(VISIBLE);
        if (imgId != 0) {
            Drawable imgDrawable = ContextCompat.getDrawable(context, imgId);
            imgDrawable.setBounds(0, 0, imgDrawable.getMinimumWidth(), imgDrawable.getMinimumHeight());
            btn_right.setCompoundDrawables(null, null, imgDrawable, null);
        }
        if (TextUtils.isEmpty(content)) {
            btn_right.setText(content);
        }
        btn_right.setOnClickListener(listener);
    }
}
